﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class SanPhamThuongDangBanTrenGianHangDAO
    {
        #region 1   -   Queries

        public static SanPhamThuongDangBanTrenGianHangDTO LaySanPhamThuongTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.ID == id);
            return ChuyenTuSanPhamThuongSangDTO(temp);
        }

        public static SanPhamThuongDangBanTrenGianHangDTO LaySanPhamThuongTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.MaSanPham == masanpham);
            return ChuyenTuSanPhamThuongSangDTO(temp);
        }

        public static List<SanPhamThuongDangBanTrenGianHangDTO> LayDanhSachSanPhamThuongTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSPThuong = from sp in myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs
                               where sp.MaSanPham == masanpham
                               select sp;

            List<SanPhamThuongDangBanTrenGianHangDTO> result = new List<SanPhamThuongDangBanTrenGianHangDTO>();
            foreach (SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG item in ListSPThuong)
            {
                SanPhamThuongDangBanTrenGianHangDTO temp = ChuyenTuSanPhamThuongSangDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static List<SanPhamThuongDangBanTrenGianHangDTO> LayDanhSachSanPhamThuongTheoMaGianHang(int magianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSPThuong = from sp in myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs
                               where sp.MaGianHang == magianhang
                               select sp;

            List<SanPhamThuongDangBanTrenGianHangDTO> result = new List<SanPhamThuongDangBanTrenGianHangDTO>();
            foreach (SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG item in ListSPThuong)
            {
                SanPhamThuongDangBanTrenGianHangDTO temp = ChuyenTuSanPhamThuongSangDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static List<SanPhamThuongDangBanTrenGianHangDTO> LayDanhSachSanPhamThuongTheoTinhTrang(bool tinhtrang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var ListSPThuong = from sp in myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs
                               where sp.TinhTrangSanPham == tinhtrang
                               select sp;

            List<SanPhamThuongDangBanTrenGianHangDTO> result = new List<SanPhamThuongDangBanTrenGianHangDTO>();
            foreach (SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG item in ListSPThuong)
            {
                SanPhamThuongDangBanTrenGianHangDTO temp = ChuyenTuSanPhamThuongSangDTO(item);
                result.Add(temp);
            }
            return result;
        }

        public static bool ThemSanPhamThuong(SanPhamThuongDangBanTrenGianHangDTO sanphamthuongDTO)
        {
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG temp = ChuyenTuDTOSangSanPhamThuong(sanphamthuongDTO);
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            try
            {
                myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.InsertOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaSanPhamThuongTheoID(int id)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.ID == id);
            try
            {
                myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaDanhSachSanPhamThuongTheoMaGianHang(int magianhang)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            var listsp = from sanphamthuongdangbantrengianhang in myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs
                         where sanphamthuongdangbantrengianhang.MaGianHang == magianhang
                         select sanphamthuongdangbantrengianhang;
            try
            {
                foreach (SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG sp in listsp)
                {
                    myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.DeleteOnSubmit(sp);
                    myDB.SubmitChanges();                    
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaSanPhamThuongTheoMaSanPham(int masanpham)
        {
            MyDataBaseDataContext myDB = new MyDataBaseDataContext();
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG temp = myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.Single(c => c.MaSanPham == masanpham);
            try
            {
                myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs.DeleteOnSubmit(temp);
                myDB.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool XoaSanPhamTrongGianHangTheoMaSanPham(int masanpham)
        {
            try
            {
                MyDataBaseDataContext myDB = new MyDataBaseDataContext();
                var lisp = from spt in myDB.SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANGs
                           where spt.MaSanPham == masanpham
                           select spt;
                foreach (SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG item in lisp)
                {
                    SanPhamThuongDangBanTrenGianHangDAO.XoaSanPhamThuongTheoID(item.ID);
                }
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }

        #endregion

        #region 2   -   Function Helper

        public static SanPhamThuongDangBanTrenGianHangDTO ChuyenTuSanPhamThuongSangDTO(SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG sanphamthuong)
        {
            SanPhamThuongDangBanTrenGianHangDTO result = new SanPhamThuongDangBanTrenGianHangDTO();
            result.GiaDangBan = sanphamthuong.GiaDangBan.Value;
            //result.ID = sanphamthuong.ID;
            result.MaGianHang = sanphamthuong.MaGianHang;
            result.MaSanPham = sanphamthuong.MaSanPham;
            result.TinhTrangSanPham = sanphamthuong.TinhTrangSanPham.Value;
            return result;
        }

        public static SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG ChuyenTuDTOSangSanPhamThuong(SanPhamThuongDangBanTrenGianHangDTO sanphamthuongDTO)
        {
            SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG result = new SAN_PHAM_THUONG_DANG_BAN_TREN_GIAN_HANG();
            result.GiaDangBan = sanphamthuongDTO.GiaDangBan;
            //result.ID = sanphamthuongDTO.ID;
            result.MaGianHang = sanphamthuongDTO.MaGianHang;
            result.MaSanPham = sanphamthuongDTO.MaSanPham;
            result.TinhTrangSanPham = sanphamthuongDTO.TinhTrangSanPham;
            return result;
        }
        #endregion
    }
}
